# TurboWiki is a simple wiki module for TurboGears
#
# Copyright (C) 2009 Alberto Granzotto <agranzot@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

'''The module contains classes and functions for identity management.

To understand how identity management is modeled for TurboWiki, you have to
know those conceps:
The possible actions with the wiki are:
    * view, that is open a page;
    * edit, that is modify or start a new page.

There are two different type of users:
    * identified users;
    * anonymous users.

Identified users can do everything.

The anonymous users' rights are managed by four configuration variables:
    * turbowiki.anonymous.view.default (default is 'accept');
    * turbowiki.anonymous.view.except (default is None);
    * turbowiki.anonymous.edit.default (default is 'accept');
    * turbowiki.anonymous.edit.except (default is None);

'default' must be either 'accept' or 'deny'.
'except' must be a comma-separated string of terms or None.

'accept' means that the default behaviour is to accept the action but
if a page starts with one of the strings specified in 'except', the
request is dropped.

'deny' means that the default behaviour is to drop the action but
if a page starts with one of the strings specified in 'except', the
request is accepted.

An example of configuration:
turbowiki.anonymous.view.default = 'accept'
turbowiki.anonymous.view.except = 'private, secret'
turbowiki.anonymous.edit.default = 'deny'
turbowiki.anonymous.edit.except = None
'''

import turbogears as tg
from turbogears import identity
from turbogears.identity import conditions
import cherrypy
import re

# look into the configuration and split the string, if any
anonymous_view_default = tg.config.get(
    'turbowiki.anonymous.view.default', 'accept') == 'accept'

anonymous_view = tg.config.get('turbowiki.anonymous.view.except', None)
if anonymous_view:
    anonymous_view = map(lambda x: x.strip(), anonymous_view.split(','))
else:
    anonymous_view = []

# look into the configuration and split the string, if any
anonymous_edit_default = tg.config.get(
    'turbowiki.anonymous.edit.default', 'accept') == 'accept'

anonymous_edit = tg.config.get('turbowiki.anonymous.edit.except', None)
if anonymous_edit:
    anonymous_edit = map(lambda x: x.strip(), anonymous_edit.split(','))
else:
    anonymous_edit = []

def check_can_view_wiki(page_name):
    '''Determine whether the visitor can view the page.

    The default behaviour is that anonymous user can view anything,
    if you don't want this modify the configuration variables:
     * turbowiki.anonymous.view.default
     * turbowiki.anonymous.view.except
    or you can redefine this function.'''
    if identity.current.anonymous:
        for ex in anonymous_view:
            # if the page name starts with an "exception"
            # return the negated default
            if page_name.startswith(ex): return not anonymous_view_default
    else:
        return True
    return anonymous_view_default

class can_view_wiki(conditions.Predicate, conditions.IdentityPredicateHelper):
    '''Predicate for checking whether the visitor can view the page.

    The default behaviour is that anonymous user can view anything,
    if you don't want this modify the configuration variables:
     * turbowiki.anonymous.view.default
     * turbowiki.anonymous.view.except
    or you can redefine this class.'''

    error_message = _('You cannot view the page')

    def __init__(self, key):
        self.key = key

    def eval_with_object(self, iden, errors=None):
        page_name = cherrypy.request.input_values[self.key]
        return check_can_view_wiki(page_name)

def check_can_edit_wiki(page_name):
    '''Determine whether the visitor can edit the page.

    The default behaviour is that anonymous user can't edit anything,
    if you don't want this modify the configuration variables:
     * turbowiki.anonymous.edit.default
     * turbowiki.anonymous.edit.except
    or you can redefine this function.'''

    if identity.current.anonymous:
        for ex in anonymous_edit:
            # if the page name starts with an "exception"
            # return the negated default
            if page_name.startswith(ex): return not anonymous_edit_default
    else:
        return True
    return anonymous_edit_default

class can_edit_wiki(conditions.Predicate, conditions.IdentityPredicateHelper):
    '''Predicate for checking whether the visitor can edit the page.

    The default behaviour is that anonymous user can edit anything,
    if you don't want this modify the configuration variables:
     * turbowiki.anonymous.edit.default
     * turbowiki.anonymous.edit.except
    or you can redefine this class.'''
    error_message = _('You cannot edit the page')

    def __init__(self, key):
        self.key = key

    def eval_with_object(self, iden, errors=None):
        page_name = cherrypy.request.input_values[self.key]
        return check_can_edit_wiki(page_name)

